package com.b.sts.service.impl;

import cn.hutool.core.util.StrUtil;
import com.b.sts.entity.CommentEntity;
import com.b.sts.entity.ProductEntity;
import com.b.sts.mapper.CommentMapper;
import com.b.sts.service.CommentService;
import com.b.sts.service.ProductService;
import com.b.sts.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 评论表 服务实现类
 * </p>
 *
 * @author b
 * @since 2023-07-12
 */
@Service
public class CommentServiceImp extends ServiceImpl<CommentMapper, CommentEntity> implements CommentService {
    @Resource
    CommentMapper mapper;
    @Resource
    UserService userService;
    @Resource
    ProductService productService;

    @Override
    public CommentEntity get(Integer id) {
        return mapper.selectById(id);
    }

    @Override
    public List<CommentEntity> list(CommentEntity entity) {
        LambdaQueryWrapper<CommentEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.setEntity(entity);
        String search = entity.getSearch();
        if (StrUtil.isNotBlank(search)) {
            wrapper.or().like(CommentEntity::getContent, search).or()
                    .like(CommentEntity::getId, search).or()
                    .like(CommentEntity::getProductId, search).or()
                    .like(CommentEntity::getUserId, search).or();
        }
        List<CommentEntity> data = mapper.selectList(wrapper);
        data.forEach(e -> {
            e.setUserEntity(userService.getById(e.getUserId()));
            e.setProductEntity(productService.getById(e.getProductId()));
        });
        return data;
    }

    @Override
    public boolean add(CommentEntity entity) {
        return mapper.insert(entity) > 0;
    }

    @Override
    public boolean del(Integer id) {
        return mapper.deleteById(id) > 0;
    }

    @Override
    public boolean update(CommentEntity entity) {
        return mapper.updateById(entity) > 0;
    }

}
